package com.hontech.bv.ui.fragment.powder

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.hontech.bv.R
import com.hontech.bv.conn.Device
import com.hontech.bv.conn.api.Powder
import com.hontech.bv.task.onClick
import com.hontech.bv.ui.window.*
import com.hontech.bv.ui.wrapDialog
import com.hontech.bv.utils.toInt

private fun showUpdownDialog() {
    ButtonDialog("测试升降电机", arrayOf(
        ButtonKv("上升") {
            wrapDialog("测试升降电机", "上升") {
                Powder.updownCtrl(0)
            }
        }, ButtonKv("下降") {
            wrapDialog("测试升降电机", "下降") {
                Powder.updownCtrl(1)
            }
        }, ButtonKv("上升200ms") {
            wrapDialog("测试升降电机", "上升200ms") {
                Powder.updownCtrl(2)
            }
        }, ButtonKv("下降200ms") {
            wrapDialog("测试升降电机", "下降200ms") {
                Powder.updownCtrl(3)
            }
        }, ButtonKv("取货") {
            wrapDialog("测试升降电机", "取货") {
                Powder.updownCtrl(4)
            }
        }
    )).show()
}

private fun showDumpDialog() {
    ButtonDialog("测试倾倒电机", arrayOf(
        ButtonKv("倾倒") {
            wrapDialog("测试倾倒电机", "倾倒") {
                Powder.dumpCtrl(0)
            }
        }, ButtonKv("复位") {
            wrapDialog("测试倾倒电机", "复位") {
                Powder.dumpCtrl(1)
            }
        }, ButtonKv("倾倒200ms") {
            wrapDialog("测试倾倒电机", "倾倒200ms") {
                Powder.dumpCtrl(2)
            }
        }, ButtonKv("复位200ms") {
            wrapDialog("测试倾倒电机", "复位200ms") {
                Powder.dumpCtrl(3)
            }
        }
    )).show()
}

private fun showMoveDialog() {
    FormDialog("测试移动电机", "测试", arrayOf(
        FormKV("位置", "0")
    )) {
        val position = it[0]
        wrapDialog("测试移动电机", "测试:${position}") {
            Powder.moveCtrl(position)
        }
    }.show()
}

private fun showCargoDialog() {
    FormDialog("测试货架电机", "测试", arrayOf(
        FormKV("位置", "0")
    )) {
        val position = it[0]
        wrapDialog("测试货架电机", "测试:${position}") {
            Powder.cargoCtrl(position)
        }
    }.show()
}

private fun showPdeDialog() {
    ButtonDialog("测试压紧电磁铁", arrayOf(
        ButtonKv("压紧") {
            wrapDialog("测试压紧电磁铁", "压紧") {
                Powder.pdeCtrl(0)
            }
        }, ButtonKv("释放") {
            wrapDialog("测试压紧电磁铁", "释放") {
                Powder.pdeCtrl(1)
            }
        }
    )).show()
}

private fun showPickDialog() {
    FormDialog("测试取货", "确定", arrayOf(
        FormKV("货道(列)", "1"),
        FormKV("加水(毫升)", "200"),
        FormKV("温度", "110")
    )) {
        val col = it[0]
        val waterMl = it[1]
        val temp = it[2]
        wrapDialog("测试取货", "列:$col 加水量:${waterMl}毫升 温度:${temp}") {
            Powder.pick(col, waterMl, temp)
        }
    }.show()
}

class CtrlDelegate (view: View) {

    private val btnUpdown = view.findViewById<Button>(R.id.id_powder_btn_updown_ctrl)
    private val btnDump = view.findViewById<Button>(R.id.id_powder_btn_dump_ctrl)
    private val btnMove = view.findViewById<Button>(R.id.id_powder_btn_move_ctrl)
    private val btnCargo = view.findViewById<Button>(R.id.id_powder_btn_cargo_ctrl)
    private val btnPde = view.findViewById<Button>(R.id.id_powder_btn_lock_ctrl)
    private val btnPick = view.findViewById<Button>(R.id.id_powder_btn_pick)


    init {
        btnUpdown.setOnClickListener { showUpdownDialog() }
        btnDump.setOnClickListener { showDumpDialog() }
        btnMove.setOnClickListener { showMoveDialog() }
        btnCargo.setOnClickListener { showCargoDialog() }
        btnPde.setOnClickListener { showPdeDialog() }
        btnPick.setOnClickListener { showPickDialog() }
    }

}
